///////////////////////////////////////////////////////////////////////////////
// File:        TcpConnectionStream.hpp
// Author:      671643387@qq.com
// Description:
///////////////////////////////////////////////////////////////////////////////

#ifndef MONKEY_COMMON_TCPCONNECTIONSTREAM_HPP_
#define MONKEY_COMMON_TCPCONNECTIONSTREAM_HPP_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include "common/Config.hpp"

namespace monkey
{
    namespace common
    {
        class TcpConnection;

        class TcpConnectionStream
        {
        protected:
            friend class TcpConnection;
        protected:
            typedef TcpConnectionStream Self;
        protected:
            TcpConnectionStream(TcpConnection& conn);
            virtual ~TcpConnectionStream();

        public:
            unsigned int Capacity(void) { return buffSize_; }
            unsigned int Size(void) { return size_; }
            unsigned int Space(void) { return buffSize_ - size_; }

        protected:
            void Reset(void)
            {
                begIndex_ = 0;
                endIndex_ = 0;
                size_ = 0;
            }

        protected:
            unsigned int Read(char* buf, unsigned int length);
            unsigned int Write(const char* buf, unsigned int length);

        protected:
            unsigned int Peek(char* buf, unsigned int length);
            unsigned int Skip(unsigned int length);

        protected:
            TcpConnection& conn_;

        private:
            unsigned int buffSize_;
            unsigned int begIndex_;
            unsigned int endIndex_;
            unsigned int size_;
            char buffer_[TCP_CONN_STREAM_SIZE];
        };
    }
}

#endif /* MONKEY_COMMON_TCPCONNECTIONSTREAM_HPP_ */
